
local locplayer = ac.player.self
local w,h = 672,536
local ui = class.panel:builder 
{
    x = (1920 - w)/2,
    y = 200,
    w = w,
    h = h,
    show_flag = false,
    normal_image = [[UI\吞噬面板\整体底框.tga]],
}
local title = ui:add_panel([[UI\吞噬面板\已吞噬技能.tga]],(ui.w-136)/2,30,136,30)
ui.count = title:add_text('(10)',title.w,0,100,nil,14,4)
ui:set_level(99)

local normal_icon = [[UI\吞噬面板\格子1.tga]]
local w1,h1 = 64,64
ui.panels = {}
for i=1,48 do
    local x = (i-1)%8 * (w1+4) + 65
    local y = (math.ceil(i/8)-1) * (h1+4) + 30 + 40
    local panel = ui:add_panel(normal_icon,x,y,w1,h1)

    panel.icon = panel:add_button('',0,0,w1,h1,1)
    
    panel.icon.frame = panel.icon:add_panel('')
    panel.icon.overlay = panel.icon:add_number_panel((w1-36)/2,h1/2,36,32)
    panel.icon.overlay:hide()

    panel.tfcard = panel:add_panel([[core\bantouming.blp]],0,0,25,15)
    panel.tfcard.text = panel.tfcard:add_text('名称',0,0,panel.tfcard.w+50,nil,6,4)

    panel.icon.index = i
    ui.panels[i] = panel
end



ui.select = ui:add_highlight([[UI\Widgets\Console\Human\CommandButton\human-activebutton.blp]],0,0,w1,h1)
ui.select:hide()

function ui:update()
    local hero = locplayer.hero
    local data = hero and hero:get_data('吞噬天赋表') or {}
    local index = (self.page - 1) * #self.panels
    self.max_page = math.max(math.ceil(#data/#self.panels),1)
    self.page_text:set_text(('%s/%s'):format(self.page,self.max_page))
    self.count:set_text(('|cff999999(|r|cff00ffff%s|r|cff999999)|r'):format(#data))
    for i,panel in ipairs(self.panels) do
        local button = panel.icon
        local skill = data[i + index]
        button.skill = skill
        if skill then
            button.frame:set_normal_image(skill.frame_art)
            button:set_normal_image(skill.art)
            if skill.card_count then
                button.overlay.text:set_text(skill.card_count)
                button.overlay:show()
            else
                button.overlay:hide()
            end
            panel.tfcard.text:set_text(skill.show_class)
            panel.tfcard:show()
        else
            button.overlay:hide()
            panel.tfcard:hide()
            button:set_normal_image('')
            button.frame:set_normal_image('')
        end
    end
end

function ui:on_button_mouse_enter(button)
    local skill = button.skill
    if skill then
        local x,y = button:get_real_position()
        self.select:set_real_position(x,y)
        self.select:show()
        local tip = skill:get_tip()
        local title = skill:get_title()
        self:tooltip(title,tip)
    end
end

function ui:on_button_mouse_leave(button)
    self.select:hide()
end

function ui:on_button_clicked(button)
    if button==self.page_add then
        self.page = math.min(self.page + 1,self.max_page)
        self:update()
    elseif button==self.page_los then
        self.page = math.max(self.page - 1,1)
        self:update()
    end
end

--[翻页]
ui:add_page_area(nil,ui.h - 45)

local button = class.button.create([[ui\吞噬按钮-c.tga]],1430,880,176*0.8,137*0.8,1)
button:set_pushedd([[ui\吞噬按钮-l.tga]])
--button:add_frame('tooltip_backdrop0')
--button:add_text('吞噬|n天赋',0,0,nil,nil,12,5)

ac['吞噬按钮'] = button

function button:on_button_mouse_enter()
    local player = ac.player.self
    local hero = player.hero
    if hero then
        local tips = {}
        table.insert(tips,('总计:%0.f'):format(hero:get('天赋数量')))
        for _,name in ipairs(ac.card_class or {}) do
            local key = ('%s-计数'):format(name)
            local num = hero:get_data(key,0)
            if num and num>0 then
                table.insert(tips,('%s:%s'):format(name,num))
            end
        end
        self:tooltip('当前计数',table.concat(tips,'|n'),'top')
    end
end

function button:on_button_clicked()
    if ui:is_show() then
        ui:hide()
    else
        ui:update()
        ui:show()
    end
end


ac.game:notify('游戏-注册显示UI',ui,true)

return button